/*
 * ARQQueue.h
 *
 *  Created on: 27.11.2012
 *      Author: mario
 */

#ifndef ARQQUEUE_H_
#define ARQQUEUE_H_

#include "messages/TransportMessage.h"
#include "User.h"

#include <map>
#include <list>
#include <vector>
#include <boost/filesystem.hpp>

using boost::filesystem::path;

struct ARQ_Queue_Entry
{
    omnibus_seq_num seq_num;
    string filename;
    string receipt;
    TMsgPtr msg;
    
    ARQ_Queue_Entry(omnibus_seq_num seq_num, string filename, string receipt, TMsgPtr msg) :
        seq_num(seq_num),
        filename(filename),
        receipt(receipt),
        msg(msg)
    {
    }
};

class ARQ_Queue
{
public:
    typedef std::list<ARQ_Queue_Entry> MessageList;
    typedef std::map<string, MessageList> PerUserMap;
    
    ARQ_Queue();
    virtual ~ARQ_Queue();
    
    /** returns next unused sequence number given user, but don't keeps track of it
     * ---> next sequence number is only calculated by stored packets
     * 
     * generates a random number if no packet is stored
     */
    omnibus_seq_num get_next_seqnum(UserPtr user);
    
    void get_waiting_messages(vector<TMsgPtr>& messages, UserPtr user);
    
    void push_message(UserPtr user, omnibus_seq_num seqnum, TMsgPtr msg);
    void remove_message(UserPtr user, omnibus_seq_num seqnum);
    
    void debug_show_queue(UserPtr user);
    
    // saves arq_queue and all waiting messages to disc
    void save_to_disk(const path& message_dir);
    
    // loads arq_queue and all waiting messages from disc
    void load_from_disk(const path& message_dir);
    
    
private:
    TMsgPtr read_message(path filename);

private:
    PerUserMap arq_map;
};

#endif /* ARQQUEUE_H_ */
